package com.xuhf.controller;

import com.alibaba.fastjson.JSONObject;
import com.xuhf.service.RabbitMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/message")
public class MesasgeController {

    @Autowired
    private RabbitMQService rabbitMQService;

    @PostMapping("/send")
    public String send() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitMQService.send(JSONObject.toJSONString(map));
        return "ok";
    }

    @PostMapping("/send2")
    public String send2(String e, String r) {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = e + " , " + r + " , hello world!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitMQService.send(e, r, JSONObject.toJSONString(map));
        return "ok";
    }

}
